﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ShevaEngine.Core.Foundation
{
    /// <summary>
    /// Triangle class.
    /// </summary>
    public class Triangle
    {
        /// <summary>Vertex 1.</summary>
        public Vector3 Vertex1;
        /// <summary>Vertex 2.</summary>
        public Vector3 Vertex2;
        /// <summary>Vertex 3.</summary>
        public Vector3 Vertex3;


        /// <summary>
        /// Constructor.
        /// </summary>
        public Triangle()
        {
            this.Vertex1 = Vector3.Zero;
            this.Vertex2 = Vector3.Zero;
            this.Vertex3 = Vector3.Zero;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="vertex1"></param>
        /// <param name="vertex2"></param>
        /// <param name="vertex3"></param>
        public Triangle(Vector3 vertex1, Vector3 vertex2, Vector3 vertex3)
        {
            this.Vertex1 = vertex1;
            this.Vertex2 = vertex2;
            this.Vertex3 = vertex3;
        }

        /// <summary>
        /// Funkce zjisti, zda-li bod lezi uvnitr trojuhelniku.
        /// </summary>
        /// <param name="vertex"></param>        
        /// <returns></returns>
        public bool ContainsVertex(Vector3 vertex)
        {
            Vector2 p = new Vector2(vertex.X, vertex.Y);
            Vector2 a = new Vector2(this.Vertex1.X, this.Vertex1.Y);
            Vector2 b = new Vector2(this.Vertex2.X, this.Vertex2.Y);
            Vector2 c = new Vector2(this.Vertex3.X, this.Vertex3.Y);

            if (vertex == this.Vertex1 || vertex == this.Vertex2 || vertex == this.Vertex3)
                return false;

            double pab = (p - a).Cross(b - a);
            double pbc = (p - b).Cross(c - b);

            if (pab * pbc < 0) return false;

            double pca = (p - c).Cross(a - c);

            if (pab * pca < 0) return false;

            return true;
        }
    }
}
